package com.app.mymusic.ui;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.app.mymusic.R;
import com.app.mymusic.db.SongDB;
import com.app.mymusic.model.Mp3Info;
import com.app.mymusic.utils.DialogUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ScanMusicActivity extends AppCompatActivity implements View.OnClickListener {


    @BindView(R.id.scan_num)
    TextView scanNum;
    private Button scanBtn;
    private SongDB songDB;
    private Dialog mDialog;
    List<Mp3Info> lists;
    private int oldNum;
    private int newNum;
    private Handler mHandler=new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(mDialog!=null)
            {
                mDialog.cancel();
            }
            switch (msg.what)
            {
                case 123:
                    scanNum.setText("新增"+(newNum-oldNum)+"首歌曲");
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_music);
        ButterKnife.bind(this);
        scanBtn = (Button) findViewById(R.id.scan_btn);
        scanBtn.setOnClickListener(this);
        songDB = new SongDB(ScanMusicActivity.this);
        lists = songDB.getListSortByCategory();
        oldNum=lists.size();
        mDialog= DialogUtil.showDialog(this,"");
    }
    @Override
    public void onClick(View v) {
        mDialog.show();
        songDB.getOriginalDatas();
        List<Mp3Info> datas = songDB.getListSortByCategory();
        lists.clear();
        lists.addAll(datas);
        newNum=lists.size();
        scanNum.setText(lists.size()+"首歌曲");
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1500);
                    Message msg=mHandler.obtainMessage();
                    msg.what=123;
                    mHandler.sendMessage(msg);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
